﻿@model RBAC.Web.Models.ListPermissionModel
@using RBAC.Web.Utilities
@using RBAC.Web.CustomHelpers
@using RBAC.Web.Mapper

@{
    ViewBag.Title = "List Permission";
    Layout = "~/Views/Shared/_Layout.cshtml"; 

    string result = Request.QueryString["result"];
    string operation = Request.QueryString["operation"];
    string messsage = "";
    bool? isSuccess = false;
    if (!String.IsNullOrEmpty(result) && !String.IsNullOrEmpty(operation))
    {
        messsage = MessageUtil.GetMessage(MessageUtil.GetOperationFromString(operation), Convert.ToBoolean(result));
        isSuccess = Convert.ToBoolean(result);
    }
    else
    {
        messsage = Model.Message;
        isSuccess = Model.IsSuccessed;

    }   
 }

@section Styles {
    <link href="@Url.Content("~/Content/themes/base/jquery-ui.css")" rel="stylesheet" type="text/css" />
    <script type="text/javascript">
        $(function () {
            $("#confirm-delete").dialog({
                autoOpen: false,
                modal:true,
                resizable: false,
                buttons: {
                    Yes: function () {
                        $(this).dialog("close");
                        $.post('@Url.Action("DeletePermissions", "Permission")',$("#main-form").serialize(), function(data) {
                            window.location.href = '@Url.Action("ListPermission", "Permission")' + '?result=' + data + '&operation=delete';
                        });
                    },
                    No:function(){
                        $(this).dialog("close");
                    }
                }
            });

            $("#dialog-alert").dialog({
                autoOpen: false,
                modal: true,
                buttons: {
                    Ok: function() {
                        $( this ).dialog( "close" );
                    }
                }
            });

            $("#button-delete").on("click", function () {
                var checkboxes = $('.grid input:checkbox');
                if(checkboxes.size() == 0)
                    $("#dialog-alert").dialog("open");
                else
                {
                    var isCheck = false;
                    for(i = 0; i < checkboxes.size(); i ++)
                    {
                        isCheck = checkboxes[i].checked;
                        if(isCheck)
                            break;
                    }

                    if(!isCheck){
                        $("#dialog-alert").dialog("open");
                    }
                    else{
                        $("#confirm-delete").dialog("open");
                       
                    }
                }
            });

            $('.grid th').each(function () {
                if (this.innerHTML == "") {
                    this.innerHTML = "<input type='checkbox' id='chkHeader'  />";
                }
            });

            $('.grid th input:checkbox').click(function (e) {
                var table = $(e.target).closest('table');
                $('td input:checkbox', table).attr('checked', e.target.checked);
            });
        });

</script>
}

<h2>Permission</h2>
@using (Html.BeginForm("ListPermission", "Permission", FormMethod.Post, new { @id = "main-form" }))
{
    <fieldset>
        <legend>Search</legend>
		
		<div class="texteditor" >
        <label>Permission Name</label>
		   @Html.TextBoxFor(model => model.PermissionNameSearch)
         </div> 
         <div class="texteditor">
            <input type="submit" id="Search" value="Search" name="submitButton" />
        </div>
    </fieldset>

	@Html.DisplayMessage(messsage, isSuccess)
    <input type="hidden" id="hid_delete" value="Delete" name="DeleteButton" />   
    <div class="top-menu">          
        <input id="add" type="button" value="New" onclick="location.href='@Url.Action("Add", "Permission")'"/>
        <input type="button" value="Delete" id="button-delete" />
    </div>
    <div id="confirm-delete" title="Confirm delete." style="display:none">
        <span>Do you want to delete?</span>
    </div>
    <div id="dialog-alert" title="Delete Permission">
      <span> No item was selected.</span>
    </div>

  if (Model.ListItems != null && Model.ListItems.Count > 0)
    {
        var grid = new WebGrid(
            source: Model.ListItems, canPage: false);
		<div style="overflow:scroll; width: 100%; height:auto; max-height: 400px;">
        @grid.GetHtml(
        tableStyle: "tableMain grid",
        rowStyle: "row-a",
        alternatingRowStyle: "row-b",
        columns: grid.Columns(
       grid.Column(header: "", style: "width_5per", format: @<text><input name="PermissionCheckbox" type="checkbox" value="@item.Id" /></text>)

              
		            ,grid.Column(header: "Permission Name", style: "width_100per", columnName: "PermissionName",
			        format: item =>
				    {
                          string link = "<a href=\"{0}?PermissionName={1}\" class=\"word_wrap\">{2}</a>";
                          return Html.Raw(string.Format(link, @Url.Content("~/Permission/EditPermission"), item.PermissionName , item.Value.PermissionName));
				    }
			   )
			,grid.Column(header: "Role Code",canSort: false, style: "width_100per", columnName: "RoleRoleCode",
				 format: item =>
				 {
					 string RoleRoleCode = "<div class=\"word_wrap\">{0}</div>";
					 return Html.Raw(string.Format(RoleRoleCode, String.IsNullOrWhiteSpace(item.RoleRoleCode+"") ? "" : item.RoleRoleCode));
				 }
			  ) 
             ,grid.Column(header: "Description", canSort: false, style: "width_100per", columnName: "Description",
				 format: item =>
				 {
					 string Description = "<div class=\"word_wrap\">{0}</div>";
					 return Html.Raw(string.Format(Description, String.IsNullOrWhiteSpace(item.Description+"") ? "" : item.Value.Description));
				 }
			  )
			,grid.Column(header: "Function",canSort: false, style: "width_100per", columnName: "FunctionFuncName",
				 format: item =>
				 {
					 string FunctionFuncName = "<div class=\"word_wrap\">{0}</div>";
					 return Html.Raw(string.Format(FunctionFuncName, String.IsNullOrWhiteSpace(item.FunctionFuncName+"") ? "" : item.FunctionFuncName));
				 }
			  ) 
 
			        ,grid.Column(header: "Create Date",canSort: false,  style: "width_100per", columnName: "CreateDate",
				        format: item =>
				        {
					         string CreateDate = "<div class=\"word_wrap\">{0}</div>"; 
					         return Html.Raw(string.Format(CreateDate, DateTime.Parse(item.CreateDate.ToString()).ToString("dd/MM/yyyy", new System.Globalization.CultureInfo("th-TH"))));
					    }
			        )
   
        ))
	</div>
    }
    else
    {
    <div class="emptyRow">No item base on search criteria that you had inputed.</div>  
    }
}

